package apis

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
)

type Favorites struct {
	api.Api
}

// GetPage 获取Favorites列表
// @Summary 获取Favorites列表
// @Description 获取Favorites列表
// @Tags Favorites
// @Param userId query int64 false "用户ID"
// @Param advertisementId query int64 false "广告ID"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Favorites}} "{"code": 200, "data": [...]}"
// @Router /api/v1/favorites [get]
// @Security Bearer
func (e Favorites) GetPage(c *gin.Context) {
	req := dto.FavoritesGetPageReq{}
	s := service.Favorites{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]models.Favorites, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Favorites失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Favorites
// @Summary 获取Favorites
// @Description 获取Favorites
// @Tags Favorites
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Favorites} "{"code": 200, "data": [...]}"
// @Router /api/v1/favorites/{id} [get]
// @Security Bearer
func (e Favorites) Get(c *gin.Context) {
	req := dto.FavoritesGetReq{}
	s := service.Favorites{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.Favorites

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Favorites失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// DeleteByUserAndAd 根据用户ID和广告ID删除收藏
// @Summary 根据用户ID和广告ID删除收藏
// @Description 根据用户ID和广告ID删除收藏
// @Tags Favorites
// @Param userId path int64 true "用户ID"
// @Param adId path int64 true "广告ID"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/favorites/user/{userId}/advertisement/{adId} [delete]
// @Security Bearer
func (e Favorites) DeleteByUserAndAd(c *gin.Context) {
	s := service.Favorites{}
	userIdStr := c.Param("userId")
	adIdStr := c.Param("adId")

	userId, err1 := strconv.ParseInt(userIdStr, 10, 64)
	adId, err2 := strconv.ParseInt(adIdStr, 10, 64)

	if err1 != nil || err2 != nil {
		e.Error(400, nil, "参数错误，用户ID或广告ID无效")
		return
	}

	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	err = (&s).DeleteByUserAndAd(userId, adId, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除收藏失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "删除成功")
}

// Insert 创建Favorites
// @Summary 创建Favorites
// @Description 创建Favorites
// @Tags Favorites
// @Accept application/json
// @Product application/json
// @Param data body dto.FavoritesInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/favorites [post]
// @Security Bearer
func (e Favorites) Insert(c *gin.Context) {
	req := dto.FavoritesInsertReq{}
	s := service.Favorites{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建Favorites失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Favorites
// @Summary 修改Favorites
// @Description 修改Favorites
// @Tags Favorites
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.FavoritesUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/favorites/{id} [put]
// @Security Bearer
func (e Favorites) Update(c *gin.Context) {
	req := dto.FavoritesUpdateReq{}
	s := service.Favorites{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Favorites失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Favorites
// @Summary 删除Favorites
// @Description 删除Favorites
// @Tags Favorites
// @Param data body dto.FavoritesDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/favorites [delete]
// @Security Bearer
func (e Favorites) Delete(c *gin.Context) {
	s := service.Favorites{}
	req := dto.FavoritesDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Favorites失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}
